<?php
function lihat(){
        $date='';
        if (isset($_POST['date'])){
            $date = $_POST['date'];
        }
        $mill = '';
        if (isset($_POST['MILLx'])){
            $mill = $_POST['MILLx'];
        }
        $txtout='';
	$txtout.="
		<form name='form' method='post' action=?>
                <div class='search-container'>
                <div class='search-side'>
		<table border='0' align='center' cellpadding='0' cellspacing='0' class='hitam'>
		<tr>
		<td align='center'>
                <label>DATE: </label><input id='datepicker' name='date' type='text' class='form' size='20' value='$date'>
                <label>&nbsp;MILL: </label>".comboODBCPDO('MASTERMILL','MILLID','MILLNAME','MILLx',"",$mill)."";

	$txtout.="</td>
		<td>&nbsp;<input type='submit' name='submit' value='GO &raquo;' class='btn-search'></td>
                </tr>
		</table>
                </div>
                </div>
                </form>";
        
        if ($date!=''){
        $arrD = explode("/", $date);
        $date = $arrD['2']."-".$arrD['1']."-".$arrD['0'];
        $arr_res = arr_result($date, $mill);
        $dataDM = array($date,$mill);
        $sqlTime = "{CALL zspICRSelectByDateMill (?,?)}";
        $res = dbresultODBCPDO($sqlTime,$dataDM);
        if($res->rowCount()=="" || $res->rowCount()==0){
            echo "<script>alert('DATA TIDAK ADA.. !!')</script>";
            echo "<script>window.history.back();</script>";
            exit();
        }
        $time = array();
        $i=1;
        while($rT = $res->fetch(PDO::FETCH_ASSOC)){
            $time['TIME'][$i] = $rT['QCTIMESTRING'];
            $time['GRIST'][$i] = $rT['ITEMNAME'];
            $time['BIN'][$i] = $rT['BIN'];
            $time['ANALYST'][$i] = $rT['NAME'];
            $time['ANALYSTID'][$i] = $rT['ANALYSTID'];
            ++$i;
        }
        $jml_analist = count($time['ANALYSTID']);
        
        $txtout.="<table id='freeze-table' border='0' bgcolor='#0006C4' class='tabel1'>
                <thead>
                <tr>
                <th colspan='2'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head'><div align='center'>TIME</div></th>";
        foreach ($time['TIME'] as $valueT) {
            $txtout.="<th width='180' class='tbl-ijo-muda'><div align='center'>$valueT</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='2'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head'><div align='center'>GRIST</div></th>";
        foreach ($time['GRIST'] as $valueG) {
            $txtout.="<th width='180' class='wrapword tbl-biru-muda'><div align='center'>$valueG</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='2'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head'><div align='center'>FLOUR BIN</div></th>";
        foreach ($time['BIN'] as $valueB) {
            $txtout.="<th width='180' class='hrf-putih'><div align='center'>$valueB</div></th>";
        }
        $txtout.="</tr>
                <tr>
                <th colspan='2'><div align='center'>&nbsp;</div></th>
		<th class='tbl-qc-head'><div align='center'>ANALYST</div></th>";
        foreach ($time['ANALYST'] as $valueA) {
            $txtout.="<th width='180' class='tbl-ijo-muda'><div align='center'>$valueA</div></th>";
        }
        $txtout.="</tr>";
        $txtout.="<tr>
		<th class='tbl-qc-head' width='80'><div align='center'>GROUP</div></th>
                <th class='tbl-qc-head' width='150'><div align='center'>DESCRIPTION</div></th>
                <th class='tbl-qc-head' width='80'><div align='center'>UNIT</div></th>
                <th class='tbl-qc-head' colspan=".count($time['TIME'])."><div align='center'>RESULT</div></th>";
        $txtout.="</tr>"
                . "</thead>"
                . "<tbody>";
        
        $sqlType = "{CALL zspICRGetQcTypeId_GB (?,?)}";
        $resType = dbresultODBCPDO($sqlType,$dataDM);
        $satu=1;
        while($rType = $resType->fetch(PDO::FETCH_ASSOC)){
            $typeid = $rType['QCTYPEID'];
            $dataDMT = array($date,$mill,$typeid);
            $sqlTU = "{CALL zspICRGetTestidUnit_GB (?,?,?)}";
            $resTU= dbresultODBCPDO($sqlTU,$dataDMT);
            $x = 1;
            $xxx = array();
            while($rTU = $resTU->fetch(PDO::FETCH_ASSOC)){
                $xxx['DESCRIPTION'][$x] = $rTU['TESTID'];
                $xxx['UNIT'][$x] = $rTU['TESTUNITID'];
                ++$x;
            }
            $jml_description = count($xxx['DESCRIPTION']);
            $txtout.="<tr>
                    <td class='tbl-abu' width='80'><div align='center'>".$typeid."</div></td>";
            $txtout.="<td class='tbl-abu' width='150'>
                    <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            foreach ($xxx['DESCRIPTION'] as $valueD){
                    $txtout.="<tr>
                              <td class='tbl-abu text-left'><div>".$valueD."</div></td>
                              </tr>
                              ";
            }
            $txtout.="</table>
                      </td>";
            
            $txtout.="<td class='tbl-abu' width='80'>
                      <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            foreach ($xxx['UNIT'] as $valueU){
                    $txtout.="<tr>
                              <td class='tbl-abu text-left'><div align='center'>".$valueU."</div></td>
                              </tr>
                              ";
            }
            $txtout.="</table>
                      </td>";
            $txtout.="<td colspan='$jml_description' class='tbl-abu'>
                      <table width='100%' border='0' bgcolor='#FFFFFF' class='tabel1'>";
            
            
            for ($a=1;$a<=$jml_description;$a++){
                $txtout.="<tr>";
                for ($b=1;$b<=$jml_analist;$b++){
                    $analyst = $time['ANALYSTID'][$b];
                    $timestring = $time['TIME'][$b];
                    $desc = $xxx['DESCRIPTION'][$a];
                    $testResult = $arr_res[$desc][$timestring][$analyst];
                    $testResult1 = $testResult==""?0:$testResult;
                    $txtout.="<td class='tbl-abu text-left' width='173'><div align='center'>".$testResult1."</div></td>";
                }
                $txtout.="</tr>";
            }
            $txtout.="</table>
                      </td>";
            $txtout.="</tr>";
            $satu++;
        }
        $txtout.="</tbody>"
                . "</table>";
        }
	return $txtout;
}

function arr_result($date,$mill){
            $dataDM = array($date,$mill);
            $sql = "{CALL zspICRGetResult_GB (?,?)}";
            $res= dbresultODBCPDO($sql,$dataDM);
            $arr_temp = array();
            while($rs = $res->fetch(PDO::FETCH_ASSOC)){
                $arr_temp[$rs['TESTID']][$rs['QCTIMESTRING']][$rs['ANALYSTID']] = $rs['TESTRESULTVALUE'];
            }
//        echo "<pre>";
//	print_r($arr_temp);
//	echo "</pre>"; 
	return $arr_temp;

}
function generate_form(){
	switch (isset($_REQUEST['mode'])) {
		default:
		$txtout=lihat();
		break;
	}
	return $txtout;
}
?>